package com.cac.demo.controller.msbinpartbasic;

import com.cac.demo.entity.msbinpartbasic.MsBinPartBasicDTO;
import com.cac.demo.service.msbinpartbasic.MsBinPartBasicService;
import com.cac.demo.util.adapter.JsonHelper;
import com.cac.demo.util.adapter.QueryReqBean;
import com.cac.demo.util.adapter.QueryRespBean;
import com.cac.demo.util.adapter.ResponseMsg;
import com.fasterxml.jackson.core.type.TypeReference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import static com.cac.demo.util.adapter.BusinessUtil.getSortExpColumnName;

@RestController
@Api(tags = "缓存料箱信息接口")
@RequestMapping("/msbinpart/")
public class MsBinPartBasicRest {
    @Autowired
    private MsBinPartBasicService msBinPartBasicService;

    /**
     * 按条件分页查询
     * @param queryReqBean
     * @return
     */
    @PostMapping("/searchByPage/v1")
    public ResponseMsg<QueryRespBean<MsBinPartBasicDTO>> searchByPage(@RequestBody QueryReqBean<MsBinPartBasicDTO> queryReqBean   ){
        System.out.println(queryReqBean.toString());
        ResponseMsg<QueryRespBean<MsBinPartBasicDTO>> responseMsg = new ResponseMsg<>();
        if(StringUtils.hasText(queryReqBean.getSidx()) && StringUtils.hasText(queryReqBean.getSord())){
            String sordExp = getSortExpColumnName(queryReqBean.getSidx(),queryReqBean.getSord(),MsBinPartBasicDTO.class);
            if(StringUtils.hasText(queryReqBean.getKeyWord())){
                queryReqBean.setSortExp(sordExp);
            }
        }
        if(StringUtils.hasText(queryReqBean.getKeyWord())){
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            MsBinPartBasicDTO searchKeyWordParam = JsonHelper.getInstance().readValue(queryReqBean.getKeyWord(), dateFormat, new TypeReference<MsBinPartBasicDTO>() {
            });
            queryReqBean.setSearchParams(searchKeyWordParam);
        }

        QueryRespBean<MsBinPartBasicDTO> result = msBinPartBasicService.searchMsBinPartBasicByPage(queryReqBean,queryReqBean.getSortExp());
        responseMsg.setResponseBody(result);
        return  responseMsg;

    }
    /**
     * 根据条件查询数据
     * @param queryReqBean
     * @return
     */
    @RequestMapping("/searchByParams/v1")
    @ApiOperation(value = "根据条件查询数据")
    public ResponseMsg<List<MsBinPartBasicDTO>> searchByParams(@ApiParam(value = "查询条件" ,name = "queryReqBean" )@RequestBody QueryReqBean<MsBinPartBasicDTO> queryReqBean){
        ResponseMsg<List<MsBinPartBasicDTO>> responseMsg = new ResponseMsg<>();
        List<MsBinPartBasicDTO> result = msBinPartBasicService.searchMsBinPartByParams(queryReqBean);
        responseMsg.setResponseBody(result);
        return  responseMsg;
    }
    /**
     * 通过主键查询单条记录
     *
     * @param id 主键id
     * @return ResponseMsg<MsBinPartBasicDTO>
     */
    @GetMapping("/get/v1/{id}")
    public ResponseMsg<MsBinPartBasicDTO> get(@PathVariable("id") String id) {
        ResponseMsg<MsBinPartBasicDTO> responseMsg = new ResponseMsg<>();
        MsBinPartBasicDTO msBinPartBasicDTO = msBinPartBasicService.findMsBinPartBasicById(id);
        if (msBinPartBasicDTO != null) {
            //valueConvert(Arrays.asList(employeeBasic1));
        }
        responseMsg.setResponseBody(msBinPartBasicDTO);
        return responseMsg;
    }

}
